﻿using MediatR;
using Newtonsoft.Json;
using PublicClass;
using PublicClass.HttpClient;
using S.Produce.Domains.CountDTO;
using S.Produce.Infrastructure;
using S.Produce.Read.Api.Application.Commands.SheepCountCommands;

namespace S.Produce.Read.Api.Application.CommandHandlers.SheepCountCommandHandlers
{
    /// <summary>
    /// 羊场实时存栏
    /// </summary>
    public class RealtimeCountCommandHandler : IRequestHandler<RealtimeCountCommand, RealtimeCount>
    {
        private readonly ServiceHttpClient serviceHttpClient;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="serviceHttpClient"></param>
        public RealtimeCountCommandHandler(ServiceHttpClient serviceHttpClient)
        {
            this.serviceHttpClient = serviceHttpClient;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<RealtimeCount> Handle(RealtimeCountCommand request, CancellationToken cancellationToken)
        {
            var countClient = await serviceHttpClient.Get("http://10.31.56.15:8090/api/SheepFile/SheepFileConditionarown");
            var info = JsonConvert.DeserializeObject<List<SheepDetail>>(countClient);
            var res = new RealtimeCount()
            {
                BreedinghenCount = info.Where(x => x.sheepType == SheepType.种母).Count(),
                DescendentCount = info.Where(x => x.sheepType == SheepType.后裔).Count(),
                SeedmaleCount = info.Where(x => x.sheepType == SheepType.种公).Count()
            };
            return res;
        }
    }
}


